#!/usr/bin/env python3
# coding=utf-8

import rclpy
from rclpy.node import Node
import module
import ActorClient


class RosNode(Node):
    def __init__(self):
        super().__init__("MRMDS_Python_Client")
        self.APIBindInit()
        self.get_logger().info("APIBind init success");
        self.publisher = self.create_publisher(module.Command, "commad", 10)
        self.subscripter = self.create_subscription(
            str, "state", self.SubscribeCallBack, 10)
        self.timer = self.create_timer(0.5, self.PublishCallback)

    def APIBindInit(self) -> bool:
        self.bind = ActorClient.ClientAPIBind()
        if not self.bind.Connect("http://192.168.2.104", 5174):
            self.get_logger().error("Connet error")
            return False
        if not self.bind.Login("admin"):
            self.get_logger().error("Login error")
            return False
        info = module.ActorInfo("Robot01", "Actor")
        if not self.bind.SetActorInfo(info):
            self.get_logger().error("Set Info error")
            return False

    def PublishCallback(self):
        command = self.bind.GetCommand()
        self.publisher.publish(command)
    
    def SubscribeCallBack(self,state:module.ActorState):
        if not self.bind.SetActorState(state):
            self.get_logger().error("Update state error")
